{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The MIT License (MIT)\n",
    "\n",
    "# Copyright (c) 2020, NVIDIA CORPORATION.\n",
    "\n",
    "# Permission is hereby granted, free of charge, to any person obtaining a copy of\n",
    "# this software and associated documentation files (the \"Software\"), to deal in\n",
    "# the Software without restriction, including without limitation the rights to\n",
    "# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n",
    "# the Software, and to permit persons to whom the Software is furnished to do so,\n",
    "# subject to the following conditions:\n",
    "\n",
    "# The above copyright notice and this permission notice shall be included in all\n",
    "# copies or substantial portions of the Software.\n",
    "\n",
    "# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n",
    "# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\n",
    "# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\n",
    "# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\n",
    "# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n",
    "# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tutorial: Feature Engineering for Recommender Systems"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. Preprocessing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In real-world applications, datasets are often messy. They are exported from production databases and often contains missing values. Treating missing values are important, as models handle them differently and some data operations/feature engineering ignore missing values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import IPython\n",
    "\n",
    "import pandas as pd\n",
    "import cudf\n",
    "import numpy as np\n",
    "import cupy\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "df_train = cudf.read_parquet('../data/train.parquet')\n",
    "df_valid = cudf.read_parquet('../data/valid.parquet')\n",
    "df_test = cudf.read_parquet('../data/test.parquet')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see, that multiple features have missing values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "event_time             0\n",
       "event_type             0\n",
       "product_id             0\n",
       "brand             946612\n",
       "price                  0\n",
       "user_id                0\n",
       "user_session          66\n",
       "target                 0\n",
       "cat_0            1524551\n",
       "cat_1            1524551\n",
       "cat_2            5058060\n",
       "cat_3           11454881\n",
       "timestamp              0\n",
       "ts_hour                0\n",
       "ts_minute              0\n",
       "ts_weekday             0\n",
       "ts_day                 0\n",
       "ts_month               0\n",
       "ts_year                0\n",
       "dtype: uint64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train.isna().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we groupby the column brand to calculate the average and count, we can see, that the missing values with 946k observations are not included. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">target</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>brand</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>samsung</th>\n",
       "      <td>0.439618</td>\n",
       "      <td>2323417</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>apple</th>\n",
       "      <td>0.421482</td>\n",
       "      <td>2031101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>xiaomi</th>\n",
       "      <td>0.339367</td>\n",
       "      <td>1082336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>huawei</th>\n",
       "      <td>0.431646</td>\n",
       "      <td>357427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>oppo</th>\n",
       "      <td>0.465100</td>\n",
       "      <td>154913</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>lg</th>\n",
       "      <td>0.347476</td>\n",
       "      <td>153196</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>lucente</th>\n",
       "      <td>0.454330</td>\n",
       "      <td>152024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sony</th>\n",
       "      <td>0.351208</td>\n",
       "      <td>140922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>artel</th>\n",
       "      <td>0.340474</td>\n",
       "      <td>110264</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>cordiant</th>\n",
       "      <td>0.247761</td>\n",
       "      <td>109872</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            target         \n",
       "              mean    count\n",
       "brand                      \n",
       "samsung   0.439618  2323417\n",
       "apple     0.421482  2031101\n",
       "xiaomi    0.339367  1082336\n",
       "huawei    0.431646   357427\n",
       "oppo      0.465100   154913\n",
       "lg        0.347476   153196\n",
       "lucente   0.454330   152024\n",
       "sony      0.351208   140922\n",
       "artel     0.340474   110264\n",
       "cordiant  0.247761   109872"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train[['brand', 'target']].groupby(['brand']).agg(['mean', 'count']).sort_values(('target', 'count'), ascending=False).head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Depending on the datatype, there are different strategies for imputing missing values.<br><br>\n",
    "\n",
    "<b>Categorical Features:</b>\n",
    "<li> Imputing categorical features is easy -  a unique category value (e.g. \"UNKNOWN\") can be imputed\n",
    "<br><br>\n",
    "<b>Important:</b>\n",
    "Before imputing the missing values, it is beneficial to create a indicator column, which indicate if the a value was imputed or not. There is maybe a underlying pattern for the missing values and models can learn the pattern."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['brand', 'user_session', 'cat_0', 'cat_1', 'cat_2', 'cat_3']\n",
    "\n",
    "for col in cols:\n",
    "    df_train['NA_' + col] = df_train[col].isna().astype(np.int8)\n",
    "    df_train[col].fillna('UNKNOWN', inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "event_time         0\n",
       "event_type         0\n",
       "product_id         0\n",
       "brand              0\n",
       "price              0\n",
       "user_id            0\n",
       "user_session       0\n",
       "target             0\n",
       "cat_0              0\n",
       "cat_1              0\n",
       "cat_2              0\n",
       "cat_3              0\n",
       "timestamp          0\n",
       "ts_hour            0\n",
       "ts_minute          0\n",
       "ts_weekday         0\n",
       "ts_day             0\n",
       "ts_month           0\n",
       "ts_year            0\n",
       "NA_brand           0\n",
       "NA_user_session    0\n",
       "NA_cat_0           0\n",
       "NA_cat_1           0\n",
       "NA_cat_2           0\n",
       "NA_cat_3           0\n",
       "dtype: uint64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train.isna().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we repeat the previous command, we can see that UNKOWN brands get calculated."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">target</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>brand</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>samsung</th>\n",
       "      <td>0.439618</td>\n",
       "      <td>2323417</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>apple</th>\n",
       "      <td>0.421482</td>\n",
       "      <td>2031101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>xiaomi</th>\n",
       "      <td>0.339367</td>\n",
       "      <td>1082336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UNKNOWN</th>\n",
       "      <td>0.301577</td>\n",
       "      <td>946612</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>huawei</th>\n",
       "      <td>0.431646</td>\n",
       "      <td>357427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>oppo</th>\n",
       "      <td>0.465100</td>\n",
       "      <td>154913</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>lg</th>\n",
       "      <td>0.347476</td>\n",
       "      <td>153196</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>lucente</th>\n",
       "      <td>0.454330</td>\n",
       "      <td>152024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sony</th>\n",
       "      <td>0.351208</td>\n",
       "      <td>140922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>artel</th>\n",
       "      <td>0.340474</td>\n",
       "      <td>110264</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           target         \n",
       "             mean    count\n",
       "brand                     \n",
       "samsung  0.439618  2323417\n",
       "apple    0.421482  2031101\n",
       "xiaomi   0.339367  1082336\n",
       "UNKNOWN  0.301577   946612\n",
       "huawei   0.431646   357427\n",
       "oppo     0.465100   154913\n",
       "lg       0.347476   153196\n",
       "lucente  0.454330   152024\n",
       "sony     0.351208   140922\n",
       "artel    0.340474   110264"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train[['brand', 'target']].groupby(['brand']).agg(['mean', 'count']).sort_values(('target', 'count'), ascending=False).head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b>Numerical Features:</b>\n",
    "<li> Imputing median for the numerical value (per group)\n",
    "<li> Imputing mean for numercial value (per group)\n",
    "<li> In some cases, we may know what value should be used as the default value (e.g. 0 for historical data or the max)\n",
    "\n",
    "<br><br>\n",
    "<b>Important:</b>\n",
    "For the same reason as in the categorical case, it is important to add a indicator column that the datapoint was imputed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In our case, we do not have missing values in the numerical column price. Therefore, we artificially inject nans and then compare the difference."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.009995587782493818"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(42)\n",
    "df_train.loc[np.random.random(df_train.shape[0])<0.01, 'price'] = None\n",
    "df_train['price'].isna().mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We calculate the median per cat_2 and merge it to the dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_median = df_train[['cat_2', 'price']].groupby('cat_2').median().reset_index()\n",
    "df_median.columns = ['cat_2', 'price_median_per_cat2']\n",
    "df_train = df_train.merge(df_median, how='left', on='cat_2')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We create an indicator column, when price was not available and then overwrite the missing values with the median."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>event_time</th>\n",
       "      <th>event_type</th>\n",
       "      <th>product_id</th>\n",
       "      <th>brand</th>\n",
       "      <th>price</th>\n",
       "      <th>user_id</th>\n",
       "      <th>user_session</th>\n",
       "      <th>target</th>\n",
       "      <th>cat_0</th>\n",
       "      <th>cat_1</th>\n",
       "      <th>...</th>\n",
       "      <th>ts_day</th>\n",
       "      <th>ts_month</th>\n",
       "      <th>ts_year</th>\n",
       "      <th>NA_brand</th>\n",
       "      <th>NA_user_session</th>\n",
       "      <th>NA_cat_0</th>\n",
       "      <th>NA_cat_1</th>\n",
       "      <th>NA_cat_2</th>\n",
       "      <th>NA_cat_3</th>\n",
       "      <th>NA_price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-12-01 12:27:02 UTC</td>\n",
       "      <td>cart</td>\n",
       "      <td>12700214</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>35.38</td>\n",
       "      <td>580243411</td>\n",
       "      <td>0cbf5e06-a782-4c74-8002-acf282026d82</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>2019</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-12-01 12:27:02 UTC</td>\n",
       "      <td>cart</td>\n",
       "      <td>12700214</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>35.38</td>\n",
       "      <td>580243411</td>\n",
       "      <td>0cbf5e06-a782-4c74-8002-acf282026d82</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>2019</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-12-01 12:27:02 UTC</td>\n",
       "      <td>cart</td>\n",
       "      <td>12700214</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>35.38</td>\n",
       "      <td>580243411</td>\n",
       "      <td>0cbf5e06-a782-4c74-8002-acf282026d82</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>2019</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-12-01 12:27:02 UTC</td>\n",
       "      <td>cart</td>\n",
       "      <td>12700214</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>35.38</td>\n",
       "      <td>580243411</td>\n",
       "      <td>0cbf5e06-a782-4c74-8002-acf282026d82</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>2019</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-12-01 12:27:02 UTC</td>\n",
       "      <td>cart</td>\n",
       "      <td>12700214</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>35.38</td>\n",
       "      <td>580243411</td>\n",
       "      <td>0cbf5e06-a782-4c74-8002-acf282026d82</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>2019</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                event_time event_type  product_id    brand  price    user_id  \\\n",
       "0  2019-12-01 12:27:02 UTC       cart    12700214  UNKNOWN  35.38  580243411   \n",
       "1  2019-12-01 12:27:02 UTC       cart    12700214  UNKNOWN  35.38  580243411   \n",
       "2  2019-12-01 12:27:02 UTC       cart    12700214  UNKNOWN  35.38  580243411   \n",
       "3  2019-12-01 12:27:02 UTC       cart    12700214  UNKNOWN  35.38  580243411   \n",
       "4  2019-12-01 12:27:02 UTC       cart    12700214  UNKNOWN  35.38  580243411   \n",
       "\n",
       "                           user_session  target    cat_0    cat_1  ... ts_day  \\\n",
       "0  0cbf5e06-a782-4c74-8002-acf282026d82       0  UNKNOWN  UNKNOWN  ...      1   \n",
       "1  0cbf5e06-a782-4c74-8002-acf282026d82       0  UNKNOWN  UNKNOWN  ...      1   \n",
       "2  0cbf5e06-a782-4c74-8002-acf282026d82       0  UNKNOWN  UNKNOWN  ...      1   \n",
       "3  0cbf5e06-a782-4c74-8002-acf282026d82       0  UNKNOWN  UNKNOWN  ...      1   \n",
       "4  0cbf5e06-a782-4c74-8002-acf282026d82       0  UNKNOWN  UNKNOWN  ...      1   \n",
       "\n",
       "   ts_month  ts_year  NA_brand  NA_user_session  NA_cat_0  NA_cat_1  NA_cat_2  \\\n",
       "0        12     2019         1                0         1         1         1   \n",
       "1        12     2019         1                0         1         1         1   \n",
       "2        12     2019         1                0         1         1         1   \n",
       "3        12     2019         1                0         1         1         1   \n",
       "4        12     2019         1                0         1         1         1   \n",
       "\n",
       "   NA_cat_3  NA_price  \n",
       "0         1         0  \n",
       "1         1         0  \n",
       "2         1         0  \n",
       "3         1         0  \n",
       "4         1         0  \n",
       "\n",
       "[5 rows x 26 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train['NA_price'] = df_train[col].isna().astype(np.int8)\n",
    "df_train.loc[df_train['price'].isna(), 'price'] = df_train.loc[df_train['price'].isna(), 'price_median_per_cat2']\n",
    "df_train.drop('price_median_per_cat2', inplace=True).head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train['price'].isna().mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b>Predicting missing values:</b>\n",
    "In [Improving Deep Learning For Airbnb Search](https://arxiv.org/abs/2002.05515), the authors propose to use a DNN for missing user engagement features of new items (listenings). New items have no historical user engagements, such as # of views, # of bookings, etc.. In the paper, they train a DNN based on the meta information, such as price, location and predict the user engagements feature. This could be interpreted in what are the expected user engagement.<br><br>\n",
    "Instead of the hand-crafted default values for missing user engagement, the authors replaced the missing values with the prediction of the DNN and showed that it reduced the error by 43% (offline test) and improved the overall bookings by 0.38% (online A/B test). "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We shutdown the kernel."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'status': 'ok', 'restart': False}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "app = IPython.Application.instance()\n",
    "app.kernel.do_shutdown(False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
